package org.hgvc.webapp.ets.jsf.beans.voucher;

import java.util.Date;
import java.util.List;

import javax.faces.event.ActionEvent;

import org.hgvc.webapp.ets.bo.Agreement;
import org.hgvc.webapp.ets.bo.Guest;
import org.hgvc.webapp.ets.bo.Member;
import org.hgvc.webapp.ets.bo.Voucher;
import org.hgvc.webapp.ets.bo.VoucherEntry;
import org.hgvc.webapp.ets.bo.VoucherEntryDetail;
import org.hgvc.webapp.ets.constant.AdminConstant;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.spring.service.VoucherService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;



public class AgentVoucherEntryBean extends AbstractBean {
	public static final String BEAN_NAME="agentVoucherEntryBean";
	
	private VoucherService voucherService;
	private VoucherEntry voucherEntry;
	private VoucherEntryDetail voucherEntryDetail;
	private String voucherEntryType;
	private boolean weekend;
	private boolean catchError;
	private Member member;
	private Agreement agreement;
	private Guest guest;
	private Voucher voucher;
	
	public Voucher getVoucher() {
		return voucher;
	}
	public void setVoucher(Voucher voucher) {
		this.voucher = voucher;
	}
	public Member getMember() {
		return member;
	}
	public void setMember(Member member) {
		this.member = member;
	}
	public Agreement getAgreement() {
		return agreement;
	}
	public void setAgreement(Agreement agreement) {
		this.agreement = agreement;
	}
	public Guest getGuest() {
		return guest;
	}
	public void setGuest(Guest guest) {
		this.guest = guest;
	}
	public boolean isWeekend() {
		return weekend;
	}
	public void setWeekend(boolean weekend) {
		this.weekend = weekend;
	}
	public VoucherEntry getVoucherEntry() {
		return voucherEntry;
	}
	public void setVoucherEntry(VoucherEntry voucherEntry) {
		this.voucherEntry = voucherEntry;
	}
	public VoucherEntryDetail getVoucherEntryDetail() {
		return voucherEntryDetail;
	}
	public void setVoucherEntryDetail(VoucherEntryDetail voucherEntryDetail) {
		this.voucherEntryDetail = voucherEntryDetail;
	}
	public String getVoucherEntryType() {
		return voucherEntryType;
	}
	public void setVoucherEntryType(String voucherEntryType) {
		this.voucherEntryType = voucherEntryType;
	}
	public void setVoucherService(VoucherService voucherService) {
		this.voucherService = voucherService;
	}
	
	public String addVoucherEntryAction(){
		System.out.println("addVoucherEntryAction()");
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		if (this.catchError)
			outcome=null;
		return outcome;
	}
	public void addVoucherEntryActionEvent(ActionEvent event){
		System.out.println("addVoucherEntryActionEvent()");
		this.catchError=false;
		try{
			this.voucherEntry=new VoucherEntry();
			if (this.voucherEntryType.equalsIgnoreCase(AdminConstant.MEMBER))
				this.voucherEntry.setAgreement(agreement);
			else if (this.voucherEntryType.equalsIgnoreCase(AdminConstant.NON_MEMBER))
				this.voucherEntry.setProspect(this.member);
			else if (this.voucherEntryType.equalsIgnoreCase(AdminConstant.GUEST))
				this.voucherEntry.setGuest(guest);
			this.voucherEntry.setCreatedBy(getThisUser());
			this.voucherEntry.setCreatedDate(new Date());
			this.voucherEntry.setIsDeleted(CommonConstant.NOT_DELETED);
			this.voucherEntryDetail.setCreatedBy(getThisUser());
			this.voucherEntryDetail.setCreatedDate(new Date());
			this.voucherEntryDetail.setIsDeleted(CommonConstant.NOT_DELETED);
			this.voucherEntryDetail.setVoucher(this.voucher);
			if (this.weekend)
				this.voucherEntryDetail.setWeekendFlag("Y");
			else
				this.voucherEntryDetail.setWeekendFlag("N");
			this.voucherEntry =  this.voucherService.saveVoucherEntry(this.voucherEntryType, this.voucherEntry, this.voucherEntryDetail);
		 	
			List<VoucherEntryDetail> list= this.voucherService.getVoucherEntryDetailList(this.voucherEntry);
			
			//refreshVoucherEntryUpdate();
			resetManagedBean(VoucherMgmtBean.BEAN_NAME);
			VoucherMgmtBean bean = (VoucherMgmtBean) getNewManagedBean(VoucherMgmtBean.BEAN_NAME);
			bean.initVoucherEntryAction();
			bean.setVoucherEntryType(this.voucherEntryType);
			bean.setVoucherEntry(this.voucherEntry);
			bean.refreshVoucherEntryUpdate();
			bean.setAgreement(this.agreement);
			bean.setGuest(this.guest);
			bean.setMember(this.member);
			bean.setVoucher(this.voucher);
			bean.setVoucherEntryDetailList(list);
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
}
